﻿using TjZheng.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using TjZheng.Framework;

namespace TjZheng.EasyUI
{
    public class QueryDropDownList : EasyUIQueryControl
    {
        /// <summary>
        /// 查询控件类型
        /// </summary>
        public QueryComboType QueryComboType { get; set; }
        public IEnumerable<SelectListItem> Options { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="QueryField">查询字段QueryField必输</param>
        /// <param name="Provider">下拉项</param>
        public QueryDropDownList(string QueryField, IEnumerable<SelectListItem> Provider)
            : this(string.Empty, QueryField, Provider)
        {
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="QueryField">查询字段QueryField必输</param>
        /// <param name="QueryBoxType">查询控件类型</param>
        /// <param name="Provider">下拉项</param>
        public QueryDropDownList(string QueryField, QueryComboType QueryComboType, IEnumerable<SelectListItem> Provider)
            : this(string.Empty, QueryField, QueryComboType, Provider)
        {
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Prefix">Model前缀</param>
        /// <param name="QueryField">查询字段QueryField必输</param>
        /// <param name="Provider">下拉项</param>
        public QueryDropDownList(string Prefix, string QueryField, IEnumerable<SelectListItem> Provider)
            : this(Prefix, QueryField, QueryComboType.ComboBox, Provider)
        {
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Prefix">Model前缀</param>
        /// <param name="QueryField">查询字段QueryField必输</param>
        /// <param name="QueryComboType">查询控件类型</param>
        /// <param name="Provider">下拉项</param>
        public QueryDropDownList(string Prefix, string QueryField, QueryComboType QueryComboType, IEnumerable<SelectListItem> Provider)
            : base(Prefix, QueryField)
        {
            if (Provider.IsNull())
            {
                Provider = new List<SelectListItem> { };
            }
            this.QueryComboType = QueryComboType;
            this.Options = Provider;
            switch (this.QueryComboType)
            {
                case QueryComboType.ComboBox:
                    this.QueryMethod = QueryMethod.Equal;
                    this.QueryDataType = QueryDataType.Int;
                    //this.EasyUIClass = "easyui-validatebox  combobox";
                    this.EasyUIClass = "easyui-validatebox combobox";
                    break;
            }
        }

        /// <summary>
        /// 渲染查询控件
        /// </summary>
        /// <returns></returns>
        public override string RenderHtml()
        {
            TagBuilder objTagBuilder = new TagBuilder("select");
            objTagBuilder.MergeAttribute("name", this.Prefix.Append(".QueryValue"));
            if (this.Id.IsNull())
            {
                this.Id = this.Prefix.Append(".QueryValue").Replace(".", "_");
            }
            objTagBuilder.MergeAttribute("id", this.Id);
            //objTagBuilder.GenerateId(this.Id);//id时有时无
            foreach (var objOption in Options)
            {
                objTagBuilder.InnerHtml += string.Format("<option value=\"{0}\"{1}>{2}</option>", objOption.Value, objOption.Selected ? " selected" : "", objOption.Text);
            }
            if (this.EasyUIClass.IsNoNull())
            {
                objTagBuilder.AddCssClass(this.EasyUIClass);
            }
            if (HtmlAttributes.IsNull())
            {
                HtmlAttributes = new Dictionary<string, object>();
            }
            List<string> dataoptionsList = new List<string>();
            object data = null;
            if (HtmlAttributes.TryGetValue("dataoptions", out data))
            {
                if (data.ToString().IsNoNull())
                {
                    dataoptionsList.Add(data.ToString());
                }
                HtmlAttributes.Remove("dataoptions");
            }
            if (HtmlAttributes.TryGetValue("data-options", out data))
            {
                if (data.ToString().IsNoNull())
                {
                    dataoptionsList.Add(data.ToString());
                }
            }
            HtmlAttributes["data-options"] = dataoptionsList.JoinString(",");
            if (HtmlAttributes.TryGetValue("class", out data))
            {
                objTagBuilder.AddCssClass(data.ToString());
                HtmlAttributes.Remove("class");
            }
            foreach (var objKey in HtmlAttributes.Keys)
            {
                objTagBuilder.MergeAttribute(objKey, HtmlAttributes[objKey].ToString());
            }
            string objDropDownList = objTagBuilder.ToString();
            if (QueryTitle.IsNoNull())
            {
                objDropDownList = string.Format("{0}:{1}", this.QueryTitle, objDropDownList);
            }
            return objDropDownList;
        }
    }
}
